package com.cssw.fyzb.wap.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.wap.model.vo.*;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wondream
 */
@Service
public class LiveService extends BaseService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private MinaAppClient busiClient;

    @Autowired
    private MinaAppClient tpsClient;

    /**
     * 踢人
     *
     * @return
     */
    public AjaxData addLiveKickOut(Long userId, Long liveId, Long koUserId) {
        Request request = new Request("AddLiveKickOut");
        request.setParameter("liveId", liveId);
        request.setParameter("koUserId", koUserId);
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request));
    }

    /**
     * 禁言
     *
     * @return
     */
    public AjaxData addLiveMute(Long liveId, Long operUserId, Long userId) {
        Request request = new Request("AddLiveMute");
        request.setParameter("liveId", liveId);
        request.setParameter("operUserId", operUserId);
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request));
    }

    /**
     * 取消踢人
     *
     * @return
     */
    public AjaxData removeLiveKickOut(Long id) {
        Request request = new Request("RemoveLiveKickOut");
        request.setParameter("id", id);
        return convertData(busiClient.send(request));
    }

    /**
     * 取消禁言
     *
     * @return
     */
    public AjaxData removeLiveMute(Long id) {
        Request request = new Request("RemoveLiveMute");
        request.setParameter("id", id);
        return convertData(busiClient.send(request));
    }

    /**
     * 获取被踢列表
     *
     * @return
     */
    public AjaxPage listLiveKickOut(Long liveId, int pageNumber, int pageSize) {
        Request request = new Request("ListLiveKickOut");
        request.setParameter("liveId", liveId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), LiveKickOutVo.class);
    }

    /**
     * 获取被踢列表
     *
     * @return
     */
    public AjaxPage listLiveMute(Long liveId, int pageNumber, int pageSize) {
        Request request = new Request("ListLiveMute");
        request.setParameter("liveId", liveId);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), LiveMuteVo.class);
    }

    /**
     * 创建房间
     *
     * @return
     */
    public AjaxData<LiveRoomVo> createLiveRoom(Long userId, String title, String thumb, String lat, String lng) {
        Request request = new Request("CreateLiveRoom");
        request.setParameter("userId", userId);
        request.setParameter("title", title);
        request.setParameter("thumb", thumb);
        request.setParameter("lat", lat);
        request.setParameter("lng", lng);
        return convertData(busiClient.send(request), LiveRoomVo.class);
    }

    /**
     * 关闭房间
     *
     * @return
     */
    public AjaxData dropLiveRoom(Long userId, Long liveId) {
        Request request = new Request("DropLiveRoom");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        return convertData(busiClient.send(request));
    }

    /**
     * 查询观众列表
     *
     * @param userId
     * @param liveId
     * @param num
     * @return
     */
    public AjaxPage<AudienceVo> listAudience(Long userId, Long liveId, Integer num) {
        Request request = new Request("ListAudience");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        request.setParameter("num", num);
        return convertPage(busiClient.send(request), AudienceVo.class);
    }

    /**
     * 主播查询观众列表
     *
     * @param userId
     * @param liveId
     * @return
     */
    public AjaxPage<AudienceVo> listAudienceByAnchor(Long userId, Long liveId) {
        Request request = new Request("ListAudienceByAnchor");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        return convertPage(busiClient.send(request), AudienceVo.class);
    }

    /**
     * 进入直播间
     *
     * @param userId
     * @param liveId
     * @return
     */
    public AjaxData<EnterLiveRoomVo> enterLiveRoom(Long userId, Long liveId) {
        Request request = new Request("EnterLiveRoom");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        return convertData(busiClient.send(request), EnterLiveRoomVo.class);
    }

    /**
     * 退出直播间
     *
     * @param userId
     * @param liveId
     * @return
     */
    public AjaxData exitLiveRoom(Long userId, Long liveId) {
        Request request = new Request("ExitLiveRoom");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        return convertData(busiClient.send(request));
    }

    /**
     * 点赞
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [userId, liveId]
     **/
    public AjaxData liveLikes(Long userId, Long liveId) {
        Request request = new Request("LiveLikes");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        return convertData(busiClient.send(request));
    }

    /**
     * 直播举报
     *
     * @return
     */
    public AjaxData addLiveReport(Long liveId, Long fromUserId, Long toUserId, String content) {
        Request request = new Request("AddLiveReport");
        request.setParameter("liveId", liveId);
        request.setParameter("fromUserId", fromUserId);
        request.setParameter("toUserId", toUserId);
        request.setParameter("content", content);
        return convertData(busiClient.send(request));
    }

    /**
     * 创建混流
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [liveId, steam, params]
     **/
    public AjaxData createMixLiveStream(String liveId, String params) {
        Request request = new Request("CreateMixLive");
        request.setParameter("liveId", liveId);
        request.setParameter("params", params);
        return convertData(busiClient.send(request));
    }

    /**
     * 取消混流
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [liveId]
     **/
    public AjaxData cancelMixLiveStream(String liveId) {
        Request request = new Request("CancelMixLive");
        request.setParameter("liveId", liveId);
        return convertData(busiClient.send(request));
    }

    /**
     * 搜索直播
     *
     * @param key
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public AjaxPage<LiveVo> searchLive(Long userId, String key, Integer pageNumber, Integer pageSize) {

        Request request = new Request("SearchLive");
        request.setParameter("userId", userId);
        request.setParameter("key", key);
        request.setParameter("pageNumber", pageNumber);
        request.setParameter("pageSize", pageSize);
        return convertPage(busiClient.send(request), LiveVo.class);
    }

    public AjaxData<LiveStatVo> getLiveStat(Long userId, Long liveId) {
        Request request = new Request("GetLiveStat");
        request.setParameter("liveId", liveId);
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), LiveStatVo.class);
    }

    public AjaxPage<LiveGiftCountVo> listLiveGiftCount(Long userId, Long liveId) {
        Request request = new Request("ListLiveGiftCount");
        request.setParameter("liveId", liveId);
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), LiveGiftCountVo.class);
    }

    public AjaxData<UploadSigVo> getVideoUploadSig() {
        Request request = new Request("GetVideoUploadSig");
        return convertData(busiClient.send(request), UploadSigVo.class);
    }

}
